package HttpServlet;

import Entity.DataDTO3;
import Entity.Userinformation;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class joinmessage {
    public joinmessage() {

    }
    static EntityManager em = Persistence.createEntityManagerFactory("MyJPADemoPU").createEntityManager();
    //绑定
    public static void join_message(DataDTO3 dataDTO3, HttpServletRequest request, HttpServletResponse response) throws IOException {

        String username=dataDTO3.getUsername();
        EntityTransaction tx = em.getTransaction();
        HttpSession session = request.getSession(true);
        Map<String, String> map = new HashMap<String, String>();
        Map<String, String> map2 = new HashMap<String, String>();
        Map<String,String> userName = (Map<String, String>) request.getSession().getAttribute("userName");
        Userinformation userinformation=em.find(Userinformation.class,userName.get("用户名"));
        //未绑定但有用户
        if(userinformation!=null) {
            if (userinformation.getUsername() != username) {
                tx.begin();
                userinformation.setStu_number(username);
                userinformation.setDataDTO3(dataDTO3);
                em.persist(dataDTO3);
                tx.commit();

                map.put("用户名", userinformation.getUsername());
                map.put("密码", userinformation.getPassword());
                map.put("学号", userinformation.getStu_number());
                map.put("手机号码", userinformation.getPhone_number());
                map.put("电子邮箱", userinformation.getEmail());
                map.put("头像", userinformation.getImg());

                if (userinformation.getDataDTO3() != null) {
                    map2.put("学号", dataDTO3.getUsername());
                    map2.put("姓名", dataDTO3.getName());
                    map2.put("学院", dataDTO3.getFaculty_title());
                }
            }
        }
        //绑定顺便创建用户
        if(userinformation==null)
        {
            Userinformation u=new Userinformation(username,"123456",username,"","","",dataDTO3);
            tx.begin();
            em.persist(u);
            em.persist(dataDTO3);
            tx.commit();

            map.put("用户名", u.getUsername());
            map.put("密码", u.getPassword());
            map.put("学号", u.getStu_number());
            map.put("手机号码", u.getPhone_number());
            map.put("电子邮箱", u.getEmail());
            map.put("头像", u.getImg());

            if (u.getDataDTO3() != null) {
                map2.put("学号", dataDTO3.getUsername());
                map2.put("姓名", dataDTO3.getName());
                map2.put("学院", dataDTO3.getFaculty_title());
            }
        }

        //HttpSession session = request.getSession(true);
        session.setAttribute("userName", map);
        session.setAttribute("userBone", map2);
        session.setAttribute("token", "not null");

        response.sendRedirect("/show.jsp");

        }
    }
